<template>
  <div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="姓名" prop="name">
        <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="uId">
        <el-input v-model.number="ruleForm.uId"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model.number="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {notNullVerify,isNunm} from '../utils/validate'

export default {
  data() {
    return {
      ruleForm: {
        name:'',
        uId: '',
        password: ''
      },
      rules: {
        name: [
          { validator: notNullVerify, trigger: 'blur' }
        ],
        uId: [
          { validator: isNunm, trigger: 'blur' }
        ],
        password: [
          { validator: isNunm, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("user/register", this.$qs.stringify(this.ruleForm))
              .then(resp=>{
                alert("学生注册成功，请登录")
                this.ruleForm = {
                  name:'',
                  uId: '',
                  password: ''
                }
                console.log(resp)
              })
              .catch(error=>{
                alert("注册失败")
                console.log(error)
              })
        } else {
          alert("要全部输入哦")
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style>

</style>